package com.mz.ozy.activity.me;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AccountBean;
import com.mz.ozy.bean.LogoutBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.mvp.presenter.CodePresenter;
import com.mz.ozy.mvp.presenter.LoginPresenter;
import com.mz.ozy.mvp.presenter.LogoutPresenter;
import com.mz.ozy.mvp.view.CodeContract;
import com.mz.ozy.mvp.view.LogoutContract;
import com.mz.ozy.utils.AppManager;
import com.mz.ozy.utils.CodeHelper;
import com.mz.ozy.utils.ToastUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONObject;

public class LogoutActivity extends BaseActivity<LogoutContract.View, LogoutPresenter> implements LogoutContract.View, CodeContract.View {

    @BindView(R.id.tv_content)
    TextView mTvcontent;
    @BindView(R.id.et_phone)
    EditText mPhone;
    @BindView(R.id.et_code)
    EditText mCode;
    @BindView(R.id.bt_code)
    TextView mBtCode;

    private CodeHelper codeHelper;
    private CodePresenter mCodePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logout);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("注销");
        String mphone = getIntent().getStringExtra("mphone");
        String txtContent = "您好，您的账户名为"+mphone+"，如果您要注销该账户，则注销后您将无法再继续使用该账户，并且您账户的所有信息都将会被删除，本账户的积分或充值余额也会被清零。因此，请您慎重！如果您账户还有余额或正在处理的订单，您可以联系客服进行处理，处理结束后再进行注销。";
        mTvcontent.setText(txtContent);

        codeHelper = new CodeHelper(this, mBtCode);
        mCodePresenter = new CodePresenter(this);
    }


    @Override
    public void getCodeSuccess(JSONObject jsonObject) {
        ToastUtils.showToast(getString(R.string.have_send_verify_code));
        codeHelper.start();  //开启倒计时
    }

    @Override
    public void onGetCodeSuccess(JSONObject jsonObject) {
        ToastUtils.showToast(getString(R.string.have_send_verify_code));
        codeHelper.start();  //开启倒计时
    }

    @Override
    public void onGetCodeFail() {
        codeHelper.reset();  //可以点击
    }

    @Override
    public void setLogoutSuccess(LogoutBean bean) {
        ToastUtils.showToast(bean.getRespMsg());
        if(bean.getRespCode().equals("00000"))
        {
            mPresenter.exitLogin(this);
        }
    }

    @Override
    public void exitLoginSuccess(JSONObject bean) {
        AppManager.getAppManager().exitLoginActivity(this);
    }

    @OnClick({R.id.bt_code,R.id.bt_logout,R.id.bt_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_code:
                String phone = mPhone.getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    ToastUtils.showToast("请输入合法的手机号码");
                    return;
                }
                mCodePresenter.onGetCode(this, phone, CodePresenter.MSG_TYPE_FIND);
                break;
            case R.id.bt_logout:
                String mphone = mPhone.getText().toString();
                String mcode = mCode.getText().toString();
                if (TextUtils.isEmpty(mphone) || mphone.length() != 11) {
                    ToastUtils.showToast("请输入合法的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(mcode)) {
                    ToastUtils.showToast("请输入验证码");
                    return;
                }
                mPresenter.setLogout(this, mphone, mcode);
                break;
            case R.id.bt_back:
                finish();
                break;
        }
    }
}